package com.booksystem.campuslibrarymanagementsystem.controller.admin;

import com.booksystem.campuslibrarymanagementsystem.commen.request.adminrequest.AnnouncementManagement.Announcementadd;
import com.booksystem.campuslibrarymanagementsystem.commen.request.adminrequest.AnnouncementManagement.Announcementlist;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Result;
import com.booksystem.campuslibrarymanagementsystem.commen.result.Verifys;
import com.booksystem.campuslibrarymanagementsystem.entity.SystemTable;
import com.booksystem.campuslibrarymanagementsystem.service.AccountTableService;
import com.booksystem.campuslibrarymanagementsystem.service.SystemTableService;
import com.booksystem.campuslibrarymanagementsystem.utils.AuditUtil;
import com.booksystem.campuslibrarymanagementsystem.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Admin")
@Api(tags = "系统公告管理")
public class AnnouncementManagementController {
    private final String idt = "admin";
    Verifys verifys = new Verifys();

    SystemTable systemTable = new SystemTable();
    @Autowired
    private AccountTableService accountTableService;
    @Autowired
    private SystemTableService systemTableService;


    /**
     * 1.公告发布。
     *
     * @return Result.success();or return Result.fail();
     * @RequestBody Announcementadd（token ，title，text）
     * 1.解析获得的token值，并保存到键值对Map-parsed中
     * 2.判断parsed是否为null
     * 3.null：返回失败信息-请输入正确token值。不为null：判断解析到的身份码是否为admin。
     * 4.不为admin；返回失败信息-身份权限不足。是admin：判断解析到的学号是否在account表中存在。
     * 5.不存在：返回失败信息-该学号不存在。存在：判断title和text是否有空值
     * 6.有空值：返回失败信息-信息不能为空，无空值：将获取到的信息就行保存至systemTable对象中并保存至数据库之后返回布尔值a
     * 7.a=false则返回失败信息-发布失败。a=true则返回成功信息-发布成功。
     */
    @PostMapping("/announcementAdd")
    @ApiOperation(value = "公告发布", notes = "公告添加并发布")
    public Result announcementAdd(@RequestBody Announcementadd announcementadd) {
        verifys = AuditUtil.verifyUtil(announcementadd.getToken(),idt,accountTableService);
        switch (verifys.getMsg()) {
            case "成功":
                if (!announcementadd.getTitle().isEmpty() && !announcementadd.getText().isEmpty()) {
                    systemTable.setTitle(announcementadd.getTitle());
                    systemTable.setText(announcementadd.getText());
                    systemTable.setPublishDate(TimeUtil.getTime());
                    boolean a = systemTableService.save(systemTable);
                    if (a) {
                        System.out.println("发布成功");
                        return Result.success("发布成功", 1);
                    } else {
                        return Result.fail("发布失败");
                    }
                } else {
                    return Result.fail("信息不能为空");
                }
            default:
                return Result.fail(verifys.getMsg());
        }
    }

    /**
     * 2.公告列表。
     *
     * @return Result.success();or return Result.fail();
     * @RequestBody Announcementlist（token）
     * 1.解析获得的token值，并保存到键值对Map-parsed中
     * 2.判断parsed是否为null
     * 3.null：返回失败信息-请输入正确token值。不为null：判断解析到的身份码是否为admin。
     * 4.不为admin；返回失败信息-身份权限不足。是admin：判断解析到的学号是否在account表中存在。
     * 5.不存在：返回失败信息-该学号不存在。存在：获取systemTable的所有数据并保存到list-systemTable中，之后判断systemTable是否为空
     * 6.为空：返回失败信息-没有公告数据，不为空：返回systemTable数据至前端
     */
    @PostMapping("/announcementList")
    @ApiOperation(value = "公告列表", notes = "获取公告列表信息")
    public Result<List<SystemTable>> announcementList(@RequestBody Announcementlist announcementlist) {
        verifys = AuditUtil.verifyUtil(announcementlist.getToken(),idt,accountTableService);
        switch (verifys.getMsg()) {
            case "成功":
                List systemTable = systemTableService.list();
                if (!systemTable.isEmpty()) {
                    System.out.println("公告列表发送成功");
                    return Result.success(systemTable, systemTable.size());
                } else {
                    return Result.fail("没有公告数据");
                }
            default:
                return Result.fail(verifys.getMsg());
        }
    }
}
